package cn.edu.xyc.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.xyc.domain.EntityPojo;
import cn.edu.xyc.domain.User;
import cn.edu.xyc.service.UserService;
import cn.edu.xyc.util.Page;

@Controller
public class UserController {

    @Autowired
    private UserService userservice;

    @RequestMapping(value = "hello")
    public void test() {
	userservice.getinfo();
	System.out.println("hello.do go ");
    }

    @RequestMapping("add")
    public String add() {
	// userservice.addinfo();

	return "success";
    }

    @RequestMapping("good")
    public String good() {
	return "good";
    }

    // 跳转到首页
    @RequestMapping(value = "index")
    public String index() {
	return "index";
    }

    @RequestMapping(value = "/main", method = RequestMethod.POST)
    public String main(@RequestParam String username,
	    @RequestParam String password, Map<String, Object> map) {
	System.out.println(username + "=================" + password);
	if (username.trim().equals("") || null == username) {
	    map.put("message", "用户名不能为空");
	    return "login";
	}
	if (password.trim().equals("") || null == password) {
	    map.put("message", "密码不能为空");
	    return "login";
	}
	User user = new User(username, password);
	User logininfo = userservice.login(user);

	map.put("user", logininfo);
	return "redirect:/admin/index.do";
    }

    @RequestMapping(value = "/admin/index")
    public String adminindex() {
	return "admin_index";
    }

    @RequestMapping(value = "/login")
    public String login() {
	return "login";
    }

    @RequestMapping(value = "/admin/list")
    public String list(Integer pagenum, Map<String, Object> m) {
	if (null == pagenum) {
	    pagenum = 1;
	}
	Page<EntityPojo> getinfobypage = this.userservice
		.getinfobypage(pagenum);
	m.put("page", getinfobypage);
	m.put("title", "this is good");

	return "list";
    }

    @RequestMapping(value = "/admin/edit/{id}")
    public String edit(@PathVariable String id,Map<String,Object> map) {
	EntityPojo entity =this.userservice.getbyid(id);
	map.put("entity",entity);
	return "edit";
    }
    
    @ResponseBody
    @RequestMapping(value = "/admin/delete/{id}")
    public Map<String,String> delete(@PathVariable("id") String id) {
	this.userservice.delete(id);
	Map<String,String> map = new HashMap<String,String>();
	map.put("msg", "删除成功");
	return map;
    }
    
    
    @RequestMapping(value = "/admin/update",method=RequestMethod.POST)
    public String update(EntityPojo enPojo) {
	this.userservice.update(enPojo);
	return "redirect:/admin/list.do";
    }
    
    @RequestMapping(value = "/admin/add")
    public String add(EntityPojo enPojo) {
	return "add";
    }
    
    
    @RequestMapping(value = "/admin/save",method=RequestMethod.POST)
    public String save(EntityPojo enPojo) {
	this.userservice.save(enPojo);
	return "redirect:/admin/list.do";
    }
    

}
